package com.wondersgroup.controller;

import com.wondersgroup.util.RsaUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/rsa")
@RestController
public class RsaController {

  /**
   * 获取第一套  公钥  前端 进行加密
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/getPublicKey")
  public Map<String,Object> getPublicKey(HttpServletRequest request, HttpServletResponse response){
     Map<String,Object> map = new HashMap<>();
     String publicKey = null;
    try {
        publicKey = RsaUtil.getPublicKeyByBase64(1);
    } catch (Exception e) {
      e.printStackTrace();
      map.put("code",500);
      map.put("msg","获取公钥异常!");
      return map;
    }
    map.put("code",200);
    map.put("publicKey",publicKey);
    return map;

  }

  /**
   *  获取 第二套 私钥 前端进行解密
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/getPrivateKey")
  public Map<String,Object> getPrivateKey(HttpServletRequest request, HttpServletResponse response){
    Map<String,Object> map = new HashMap<>();
    String privateKey = null;
    try {
      privateKey = RsaUtil.getPrivateKeyByBase64(2);
    } catch (Exception e) {
      e.printStackTrace();
      map.put("code",500);
      map.put("msg","获取私钥钥异常!");
      return map;
    }
    map.put("code",200);
    map.put("privateKey",privateKey);
    return map;

  }


}
